/* Copyright (c) 2010 Matthew Jack 
matthew.jack+mcover@gmail.com / www.matthewjack.net

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

// Code Coverage Marker class
//
// Each Marker instance registers with the Tracker singleton when first hit
// Usage should always be via the macro defined below
//
// Advanced: The Reset functionality adds an extra static variable and an extra check to the assembler.
//           While included here for flexibility, you might choose to drop the Reset feture, roll Hit() into the
//           constructor, and thus reduce the footprint of your Markers

#ifndef CODECOVERAGEMARKER_H
#define CODECOVERAGEMARKER_H

#include "CodeCoverageTracker.h"

// Enable Code Coverage in this project
// This implementation aims to be efficient enough to be enabled for all internal builds, not just debug
#define CODE_COVERAGE_ENABLED


// Marker class, written for fast common case with minimal code bloat
class CCodeCoverageMarker
{
  friend class CCodeCoverageTracker;
public:

  // Constructor assigning text Label
  CCodeCoverageMarker( const char * sLabel )
  : m_sLabel(sLabel), m_bHit(false) {};

  // Called every time we execute code containing a Marker
  // Registers with the Tracker only on first Hit()
  // By separating this from the constructor, we allow Reset functionality
  inline void Hit()
  {
    if (!m_bHit)
    {
      m_bHit = true;
      CCodeCoverageTracker::GetInstance().Register(this);
    }
  }

  // Fetch label - useful to Manager class
  const char * GetLabel() const 
  {
    return m_sLabel; 
  }

protected:
  const char * m_sLabel;               // The text label of this marker
  bool m_bHit;                         // Cache whether marker is already registered
};

#ifdef CODE_COVERAGE_ENABLED
  #define CCMARKER( label ) \
    do { \
        static CCodeCoverageMarker ccMarker_##label( #label ); \
        ccMarker_##label.Hit(); \
    }while(0)
#else
  #define CCMARKER( label ) ((void)0)
#endif

#endif //CODECOVERAGEMARKER_H
